package com.hundredapp.oa.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaSecuit;
import com.hundredapp.oa.repository.SecuitManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Secuit extends ActionSupport {

	private List<OaSecuit> root;
	
	private Object success;
	private String msg;
	private int id;
	private int start;
	private int limit;
	private int total;
	private String searchname;
	private String searchschool;
	private OaSecuit secuit;
	
	@Autowired
	private SecuitManager secuitManager;
	
	public String add() {
		try{
			secuit.setCreateUserId(WebUtil.GetLoginUserId());
			secuitManager.add(secuit);
			success = true;
			msg = "添加成功";
		}catch(Exception e){
			success = false;
			msg = "添加失败";
		}
		return SUCCESS;
	}
	
	public String allSecuit() {
		String condition = SelectType.SELECT_ALL;
		if(searchname != null || searchschool != null) {
			searchname = JSEscape.unescape(searchname);
			searchschool = JSEscape.unescape(searchschool);
			if(searchname!=null)
				condition += " and displayName like '%" +searchname+"%' ";
			if(searchschool!=null)
				condition += " and school like '%" +searchschool+"%' ";
		}
		root=secuitManager.findAllSecuit(condition,start, limit);
		total=secuitManager.count(condition);
		return SUCCESS;
	}
	
	public String find() {
		secuit=secuitManager.findSecuitById(id);
		return SUCCESS;
	}
	
	public String update() {
		try{
			secuit.setId(id);
			secuitManager.update(secuit);
			success=true;
			msg="修改成功";
		}catch(Exception e){
			System.out.println(e.getMessage());
			success=false;
			msg="修改失败";
		}
		return SUCCESS;
	}
	
	public List<OaSecuit> getRoot() {
		return root;
	}
	public Object getSuccess() {
		return success;
	}
	public String getMsg() {
		return msg;
	}
	public void setId(int id) {
		this.id = id;
	}
	public OaSecuit getSecuit() {
		return secuit;
	}
	public void setSecuit(OaSecuit secuit) {
		this.secuit = secuit;
	}

	public void setRoot(List<OaSecuit> root) {
		this.root = root;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getTotal() {
		return total;
	}

	public String getSearchname() {
		return searchname;
	}

	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}

	public String getSearchschool() {
		return searchschool;
	}

	public void setSearchschool(String searchschool) {
		this.searchschool = searchschool;
	}
}
